<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 16:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
class Teacher extends Base
{
    /**
     * 用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['user_status'] = ['neq', 3];
            $where['user_type'] = 2;
            if(session('groupid') == 2){
                $where['user_piano'] = ['like','%' . session('piano') . '%'];
            }
            $keyword = input('keyword');
            $user_school = input('user_school');
            $sex = input('sex');
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_username|user_phone'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($user_school) && $user_school != "")
            {
                $where['user_piano'] = ['like','%'.$user_school.'%'];
            }
            //性别
            if(isset($sex) && $sex != "")
            {
                $where['user_gender'] = $sex;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_time desc')
                ->select();
            foreach ($lists as $key => $vo){
                $lists[$key]['axes'] = '';
                $lists[$key]['panios'] = '';
                $yueqi = Db::table('up_axe')->where('axe_id','in',trim($vo['user_like_axe'],','))->column('axe_name');
                if($yueqi){
                    $lists[$key]['axes'] = implode($yueqi,',');
                }
                $panios = Db::table('up_piano')->where('pa_id','in',trim($vo['user_piano'],','))->column('pa_name');
                if($panios){
                    $lists[$key]['panios'] = implode($panios,',');
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $panio = Db::table('up_piano')
            ->select();
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
        }
        $this->assign([
            'panio' => $panio,
        ]);
        return $this->fetch();
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function down(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function up(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function deluser(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 添加教师
     * @return mixed
     */
    public function teacherAdd(){
        if(Request::instance()->isPost()){
            $time = date('Y-m-d H:i:s');
            $user_image = input('user_image');
            $user_username = input('user_username');
            $user_phone = input('user_phone');
            $user_password = input('user_password');
            $user_gender = input('user_gender');
            $user_birthday = input('user_birthday');
            $user_sign = input('user_sign');
            $user_status = input('user_status');
            $user_piano = input('user_piano');
            if(!preg_match("/^1[123456789]\d{9}$/", $user_phone)){
                return json(['code' => 100, 'msg' => '请输入正确的手机号']);
            }
            $have1 = Db::name('u_user')
                ->where('user_phone',$user_phone)
                ->find();
            if($have1){
                return json(['code' => 100, 'msg' => '已经有此手机号了']);
            }
            $token = md5(date('YmdHis').rand(100000,999999));
            $update = [
                'user_image' => $user_image,
                'user_username' => $user_username,
                'user_phone' => $user_phone,
                'user_password' => md5($user_password),
                'user_gender' => $user_gender,
                'user_birthday' => $user_birthday,
                'user_sign' => $user_sign,
                'user_token' => $token,
                'user_status' => $user_status,
                'user_piano' => $user_piano,
                'user_update_time' => $time,
                'user_time' => $time,
                'user_type' => 2,
            ];
            $res = Db::name('u_user')->insert($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $panio = Db::table('up_piano')->select();
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
        }
        $this->assign('panio', $panio);
        return $this->fetch('teacher/addtea');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function memberEdit(){
        $user_id = input('user_id');
        $info = Db::name('u_user')
            ->where('user_id', $user_id)
            ->find();
        if(Request::instance()->isPost()){
            $user_image = input('user_image');
            $user_username = input('user_username');
            $user_phone = input('user_phone');
            $user_password = input('user_password');
            $user_gender = input('user_gender');
            $user_birthday = input('user_birthday');
            $user_sign = input('user_sign');
            $user_like_axe = input('user_like_axe');
            $user_piano = input('user_piano');
            if(!preg_match("/^1[123456789]\d{9}$/", $user_phone)){
                return json(['code' => 100, 'msg' => '请输入正确的手机号']);
            }
            $update = [
                'user_image' => $user_image,
                'user_username' => $user_username,
                'user_phone' => $user_phone,
                'user_gender' => $user_gender,
                'user_birthday' => $user_birthday,
                'user_sign' => $user_sign,
                'user_like_axe' => $user_like_axe,
                'user_piano' => $user_piano,
                'user_update_time' => date('Y-m-d H:i:s')
            ];
            if(isset($_POST['user_other_axe']) && $_POST['user_other_axe']){
                $update['user_other_axe'] = $_POST['user_other_axe'];
            }
            if($user_password){
                $update['user_password'] = md5($user_password);
            }
            if($info['user_phone'] != $user_phone){
                $have1 = Db::name('u_user')
                    ->where('user_phone',$user_phone)
                    ->find();
                if($have1){
                    return json(['code' => 100, 'msg' => '已经有此手机号了']);
                }
            }
            $res = Db::name('u_user')->where('user_id',$user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info['user_like_axe'] = explode(',',trim($info['user_like_axe'],','));
        $info['user_piano'] = explode(',',trim($info['user_piano'],','));
        $panio = Db::table('up_piano')->select();
        $axe = Db::table('up_axe')->select();
        $this->assign('info', $info);
        $this->assign('user_id', $user_id);
        $this->assign('panio', $panio);
        $this->assign('axe', $axe);
        return $this->fetch('teacher/memberdetail');
    }
    /**
     * 收到的评价
     * @return mixed|\think\response\Json
     */
    public function comments(){
        $user_id = input('user_id');
        if(request()->isAjax()){
            $where = [
                'plan_online' => ['<>',3],
                'plan_teacher' => $user_id
            ];
            $keyword = input('keyword');
            if(isset($keyword) && $keyword != ''){
                $where['plan_comment_content'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'p.*,user_username,user_phone,p_name';
            $count =  Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u','p.plan_student=u.user_id','LEFT')
                ->join('up_plan pl','p.plan_p_id=pl.p_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists =  Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u','p.plan_student=u.user_id','LEFT')
                ->join('up_plan pl','p.plan_p_id=pl.p_id','LEFT')
                ->where($where)
                ->order('plan_online asc,plan_comment DESC')
                ->page($NowPage,$limits)
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $this->assign('user_id',$user_id);
        return $this->fetch();
    }
    /**
     * 课程表
     * @return mixed|\think\response\Json
     */
    public function planinfos(){
        $user_id = input('user_id');
        if(request()->isAjax()){
            $where = [
                'plan_online' => ['<>',3],
                'plan_teacher' => $user_id
            ];
            $keyword = input('keyword');
            if(isset($keyword) && $keyword != ''){
                $where['user_username'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'p.*,user_username,user_phone,p_name';
            $count =  Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u','p.plan_student=u.user_id','LEFT')
                ->join('up_plan pl','p.plan_p_id=pl.p_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists =  Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u','p.plan_student=u.user_id','LEFT')
                ->join('up_plan pl','p.plan_p_id=pl.p_id','LEFT')
                ->where($where)
                ->order('plan_online asc,plan_status DESC')
                ->page($NowPage,$limits)
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $this->assign('user_id',$user_id);
        return $this->fetch();
    }
    /**
     * 在途
     * @return mixed|\think\response\Json
     */
    public function zaitu(){
        if(request()->isAjax()){
            $where = [];
            $where['plan_map'] = ['<>',''];
            if(session('groupid') == 2){
                $where['u1.user_piano'] = ['like','%' . session('piano') . '%'];
            }
            $keyword = input('keyword');
            $start = input('res_start');
            $end = input('res_end');
            if(isset($keyword) && $keyword != ''){
                $where['u1.user_username'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['plan_start_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['plan_start_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['plan_start_time'] = ['between',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'u1.user_username teaname,u2.user_username stuname,p.plan_start_time,p.plan_address,p.plan_map,p.plan_id';
            $count = Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u1','p.plan_teacher=u1.user_id','LEFT')
                ->join('up_u_user u2','p.plan_student=u2.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u1','p.plan_teacher=u1.user_id','LEFT')
                ->join('up_u_user u2','p.plan_student=u2.user_id','LEFT')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('plan_start_time asc')
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
}